﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace com.yd.sdk.api
{
    public class AnalysicsManager : MonoBehaviour,IAnalysics,IAnalysicsListener
    {
        [SerializeField]
        private string AppsFlyer = "HnmknNdxpFgdLcWhZbg5xi";//dancing hunt

        [SerializeField] 
        private string Adjust = "cu2n93esib5s";
        
        
        public bool isAutoInitialized = false;
        
        
        
        
        #region Java回调Wrapper

        //调用payManager的类
        private AndroidJavaObject _javaAnalysicsManager;
        private AndroidJavaObject JavaAnalysicsManager {
            get
            {
                #if UNITY_EDITOR
                    Debug.Log("JavaAnalysicsManager=>Editor not support");        
                #elif UNITY_ANDROID
                    _javaAnalysicsManager = _javaAnalysicsManager ?? JavaActivity.GetStatic<AndroidJavaObject>("analysisManager");
                #elif UNITY_IPHONE || UNITY_IOS
                                                        
                #else
                                                        
                #endif
                
                return _javaAnalysicsManager;
            }
        }

        private AndroidJavaClass _javaActivity;
        private AndroidJavaClass JavaActivity
        {
            get
            {
                _javaActivity = _javaActivity ?? new AndroidJavaClass(Const.MyUnityPlayerActivity);
                return _javaActivity;
            }
        }
        
        #endregion

        private void Start()
        {
            if (isAutoInitialized)
            {
                //默认去调用
                // Initialized(Const.Analysis_AppsFlyer, AppsFlyer);
                //Initialized(Const.Analysis_Adjust, Adjust);
                Initialized(Const.Analysis_Firebase, "");
            }

                
            DontDestroyOnLoad(gameObject);
        }


        // public void LogEvent(string name, string key, string value)
        // {
        //     Debug.Log("LogEvent:" + (JavaAnalysicsManager == null));
        //     JavaAnalysicsManager?.Call("LogEvent",name,key,value);
        // }
        //
        //
        public void LogEventTest()
        {
            LogEvent(Const.Analysis_Firebase, "test1name", "test1key", "test1value");
            // LogEvent("test","testkey","testvalue");
        }

        public void LogEvent(string identify, string name, string key, string value)
        {
            #if UNITY_EDITOR
                Debug.Log("LogEvent=>Editor not support");        
            #elif UNITY_ANDROID
                Debug.Log("Unity调用了LogEvent");
                JavaAnalysicsManager?.Call("LogEvent",identify,name,key,value);
            #elif UNITY_IPHONE || UNITY_IOS
                                                                    
            #else
                                                                    
            #endif
            
        }

        public void Initialized(string identify, string value)
        {
            #if UNITY_EDITOR
                Debug.Log("Initialized=>Editor not support");        
            #elif UNITY_ANDROID
                Debug.Log("Unity调用了Initialized");
                JavaAnalysicsManager?.Call("Initialized",identify,value);
            #elif UNITY_IPHONE || UNITY_IOS
                                                                    
            #else
                                                                    
            #endif
            
        }
    }
}


